package com.wushi.lenovo.asia5b.app_di.goods.presenter;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mvp.base.BasePresenter;
import com.mvp.base.model.ResultModel;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.app_di.goods.mvpview.OrderCheckCommonView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_http.OrderService;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_model.profile.AddressModel;
import com.wushi.lenovo.asia5b.app_model.result.AddressResModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;

/**
 * Created by zhangyuncai on 2020/3/10.
 * 订单校验公共页面
 */
public class OrderCheckCommonPresenter<T extends OrderCheckCommonView> extends BasePresenter<T> {

    final ApiService apiService;
    final OrderService orderService;

    public final List<Integer> idList = new ArrayList<>();
    //收货地址
    public AddressModel addressModel;

    public List<Integer> addGoods(List<Integer> goods) {
        this.idList.clear();
        if (goods != null && goods.size() > 0) {
            this.idList.addAll(goods);
        }
        return goods;
    }

    public OrderCheckCommonPresenter(ApiService apiService, OrderService orderService) {
        this.apiService = apiService;
        this.orderService = orderService;
    }


    /**
     * 扩展加入地址参数
     * <p>
     * 代购-bill
     * 自营-ship
     * 打包-ship
     * 二次/补差价-bill
     *
     * @param extraMap
     * @param isDefaultBill 是否拿默认的账单地址,根据以上原则
     * @return
     */
    public Observable<Integer> getAddressOb(MapHelper extraMap, boolean isDefaultBill) {
        if (addressModel != null) {
            Map<String, Object> deliveryAddress = getDeliveryAddress(addressModel);
            extraMap.put("delivery_address", deliveryAddress);
            extraMap.put("billing_address", deliveryAddress);
            return Observable.just(1);
        }

        return apiService.getAddresses(UserInfo.getMemberId())
                .flatMap(new Function<ResultModel<AddressResModel>, ObservableSource<Integer>>() {
                    @Override
                    public ObservableSource<Integer> apply(ResultModel<AddressResModel> addressResModelResultModel) throws Exception {
                        AddressResModel addressResModel = addressResModelResultModel.getData();
                        if (addressResModelResultModel.isSuccess() && addressResModel != null) {
                            int default_address_id = 0;
                            if (isDefaultBill) {
                                default_address_id = addressResModel.getBill_address_id();
                            } else {
                                default_address_id = addressResModel.getShip_address_id();
                            }
                            List<AddressModel> addresses = addressResModel.getAddresses();
                            if (addresses != null && addresses.size() > 0) {
                                if (addressModel == null && default_address_id > 0) {
                                    for (AddressModel model : addresses) {
                                        if (model.getId() == default_address_id) {
                                            addressModel = model;
                                            break;
                                        }
                                    }
                                }
                                if (addressModel == null) {//无默认,取第一个
                                    addressModel = addresses.get(0);
                                }
                            }

                        }
                        //todo 传回UI
                        Observable.just(1)
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(s -> {
                                    getView().getAddressModelResult(addressModel);
                                }, throwable -> {
                                    throwable.printStackTrace();
                                });
                        if (addressModel != null) {
                            Map<String, Object> deliveryAddress = getDeliveryAddress(addressModel);
                            extraMap.put("delivery_address", deliveryAddress);
                            extraMap.put("billing_address", deliveryAddress);
                        } else {
                            extraMap.put("delivery_address", "null");
                            extraMap.put("billing_address", "null");
                        }
                        return Observable.just(1);
                    }
                });
    }

    /**
     * 拼接商品id参数
     *
     * @param idList
     * @return
     */
    public String getProducts3(List<Integer> idList) {
        JsonArray jsonElements = new JsonArray();
        for (Integer id : idList) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("id", id);
            jsonElements.add(jsonObject);
        }
        return jsonElements.toString();
    }


    /**
     * 拼接地址参数
     *
     * @param addressModel
     * @return
     */
    public Map<String, Object> getDeliveryAddress(AddressModel addressModel) {
        Map<String, Object> map = MapHelper.newHashMap();
        if (addressModel != null) {
            map.put("name", addressModel.getContact_name() + "");
            map.put("address", addressModel.getAddress() + "");
            map.put("mobile", addressModel.getContact_no() + "");
            map.put("building_name", addressModel.getBuilding_name() + "");
            map.put("calling_code", addressModel.getCalling_code() + "");
            map.put("city", addressModel.getCity() + "");
            map.put("city_id", addressModel.getCity_id() + "");
            map.put("country_id", addressModel.getCountry_id() + "");
            map.put("state_id", addressModel.getState_id() + "");
            map.put("state", addressModel.getState() + "");
            map.put("address_id", addressModel.getId() + "");
            map.put("postcode", addressModel.getPostcode_no() + "");
            map.put("other_postcode", addressModel.getOther_postcode() + "");
        }
        return map;
    }

    /**
     * 拼接商品id参数
     *
     * @param goods
     * @return
     */
    public String getProductsForMethod3(List<Integer> goods) {
        JsonArray jsonElements = new JsonArray();
        for (Integer id : goods) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("id", id);
            jsonElements.add(jsonObject);
        }
        return jsonElements.toString();
    }

    /**
     * 拼接地址参数
     *
     * @param addressModel
     * @return
     */
    public JsonObject getExtra_dataForDelivery(AddressModel addressModel) {
        JsonObject extra_data = new JsonObject();
        JsonObject address = new JsonObject();
        if (addressModel != null) {
            address.addProperty("name", addressModel.getContact_name());
            address.addProperty("address", addressModel.getAddress());
            address.addProperty("mobile", addressModel.getContact_no());
            address.addProperty("building_name", addressModel.getBuilding_name());
            address.addProperty("calling_code", addressModel.getCalling_code());
            address.addProperty("city", addressModel.getCity());
            address.addProperty("city_id", addressModel.getCity_id());
            address.addProperty("country_id", addressModel.getCountry_id());
            address.addProperty("state", addressModel.getState());
            address.addProperty("state_id", addressModel.getState_id());
            address.addProperty("address_id", addressModel.getId());
            address.addProperty("postcode", addressModel.getPostcode_no());
            address.addProperty("other_postcode", addressModel.getOther_postcode());
            extra_data.add("delivery_address", address);
        }
        return extra_data;
    }


}
